package edu.scau.DAO_mode.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

import edu.scau.DAO_mode.model.User;
import edu.scau.DAO_mode.serviceimp.UserServiceImp;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UpdateDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField usernameTextField;
	private JPasswordField passwordField;
	private JPasswordField rePasswordField;


	/**
	 * Create the dialog.
	 */
	public UpdateDialog(final User user) {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel label = new JLabel("用户名");
		label.setBounds(93, 52, 54, 15);
		contentPanel.add(label);
		
		JLabel label_1 = new JLabel("密码");
		label_1.setBounds(93, 106, 54, 15);
		contentPanel.add(label_1);
		
		JLabel label_2 = new JLabel("确认密码");
		label_2.setBounds(93, 157, 54, 15);
		contentPanel.add(label_2);
		
		usernameTextField = new JTextField();
		usernameTextField.setBounds(189, 49, 66, 21);
		contentPanel.add(usernameTextField);
		usernameTextField.setColumns(10);
		usernameTextField.setText(user.getUserName());
		
		passwordField = new JPasswordField();
		passwordField.setBounds(189, 103, 66, 21);
		contentPanel.add(passwordField);
		
		rePasswordField = new JPasswordField();
		rePasswordField.setBounds(189, 154, 66, 21);
		contentPanel.add(rePasswordField);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("确认");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
					if (passwordField.getText().equals(rePasswordField.getText())){
						UserServiceImp userServiceImp;
						try {
							userServiceImp = new UserServiceImp();
							User user_new=new User(user.getID(),usernameTextField.getText(),passwordField.getText());
							if (userServiceImp.updateUser(user_new)) JOptionPane.showMessageDialog(null, "successful");
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					else JOptionPane.showMessageDialog(null, "no match");

					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("取消");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
